//
// Created by jiangpenghui on 2023/4/14.
//

#include <iostream>
#include <thread>
#include "task_thread.h"
#include <exception>
#include <future>

void hello() {
    std::cout << "Hello Concurrent World" << std::endl;
}

int main() {


    std::thread t(hello);
    // main线程等待 t线程执行完
    std::cout << "Main is run" << std::endl;

    t.join();

    std::cout << "starting func1() in background " << " and func2() in foreground:" << std::endl;

    // 异步执行 func1
    std::future<int> result1(std::async(func1));

    // func2 主线程执行，需要等待
    int result2 = func2();

    auto result3 = std::async(std::launch::async, func1);

    std::cout << "result3.get() " << result3.get() << std::endl;

    std::cout << "\nresult of func1() + func2() : " << result1.get() + result2 << std::endl;


    auto task_1_result = std::async(task_1);

    try {
        task_1_result.get();
    } catch (const std::exception &e) {
        std::cerr << "EXCEPTION：" << e.what() << std::endl;
    }

}